package com.example.memo;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.memo.Bean.noteBean;
import com.example.memo.Bean.waitBean;
import com.example.memo.R;
import com.example.memo.Services.ResultListener2;
import com.example.memo.addNoteFragment;
import com.example.memo.db.MysqlOpenHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Myadapter2 extends BaseAdapter  implements ResultListener2 {

    private List<waitBean> data;

    private String str;

    private ResultListener2 resultListener2;


    public Myadapter2(List<waitBean> data, Context context,ResultListener2 resultListener2) {
        this.data = data;
        this.context = context;
        this.resultListener2=resultListener2;
    }



    public void setData(List<waitBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }
    private Context context;

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        //使用viewHolder 绑定每个item中的控件id
        ViewHolder viewHolder;
        //每个item都要创建，
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.simple_item2, viewGroup, false);
            //绑定item下的控件id
            viewHolder = new ViewHolder();
            viewHolder.datewait= view.findViewById(R.id.datewait);
            viewHolder.waitTitle= view.findViewById(R.id.waitTitle);
            viewHolder.type= view.findViewById(R.id.datetype1);
            viewHolder.chekbox = view.findViewById(R.id.edit_type);
            view.setTag(viewHolder);
        }
        else {
            viewHolder =(ViewHolder) view.getTag();
        }
        //绑定对应的id
        viewHolder.chekbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(data.get(position).getType().equals("0")){
                    //插入数据时注意，一定要传入最新的数据库版本号
                    MysqlOpenHelper mysqlOpenHelper=new MysqlOpenHelper(context);
                    SQLiteDatabase sqLiteDatabase=mysqlOpenHelper.getWritableDatabase();
                    ContentValues values= new ContentValues();
                    values.put("type",1);
                    values.put("update_time", String.valueOf(new Date()));
                    viewHolder.type.setText("已完成");
                    sqLiteDatabase.update("wait_table",values,"id=?",new String[]{data.get(position).getId()});
                    Toast.makeText(context,"添加至已完成",Toast.LENGTH_SHORT).show();
                    viewHolder.chekbox.setEnabled(true);
                    data.remove(position);
                    //重新获取type0的数组,去更新wait_data更新视图
                    data=getData();
                  List<waitBean>  data2=getData2();
                    resultListener2.getWaitData(data);
                    resultListener2.getWaitData2(data2);
                }else if(data.get(position).getType().equals("1")){
                    //插入数据时注意，一定要传入最新的数据库版本号
                    MysqlOpenHelper mysqlOpenHelper=new MysqlOpenHelper(context);
                    SQLiteDatabase sqLiteDatabase=mysqlOpenHelper.getWritableDatabase();
                    ContentValues values= new ContentValues();
                    values.put("type",0);
                    values.put("update_time", String.valueOf(new Date()));
                    viewHolder.type.setText("待办");
                    sqLiteDatabase.update("wait_table",values,"id=?",new String[]{data.get(position).getId()});
                    Toast.makeText(context,"添加至待办",Toast.LENGTH_SHORT).show();
                    viewHolder.chekbox.setEnabled(true);
                    data.remove(position);
                    //重新获取 type1的数组  去更新wait_data2视图
                    data=getData2();
                    List<waitBean> data2=getData();
                    resultListener2.getWaitData2(data);
                    resultListener2.getWaitData(data2);
                }else {
                    Toast.makeText(context, "没有这选项", Toast.LENGTH_SHORT).show();
                }
            }
        });

        if(data.get(position).getType().equals("1")){
            viewHolder.waitTitle.setText(data.get(position).getNeirong());
            viewHolder.datewait.setText(data.get(position).getCreate_time());
            viewHolder.type.setText("已完成");
        }
        else {
            viewHolder.waitTitle.setText(data.get(position).getNeirong());
            viewHolder.datewait.setText(data.get(position).getCreate_time());
            viewHolder.type.setText("待办");
        }

        //点击
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               showCustomDialog(position);
                }

        });


        //长按删除
       view.setOnLongClickListener(new View.OnLongClickListener() {
           @Override
           public boolean onLongClick(View view) {
               MysqlOpenHelper mysqlOpenHelper = new MysqlOpenHelper(context);
               SQLiteDatabase sqLiteDatabase = mysqlOpenHelper.getWritableDatabase();
               sqLiteDatabase.delete("wait_table","id=?",new String[]{data.get(position).getId()});
               Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
               data.remove(position);
               notifyDataSetChanged();
               resultListener2.getWaitData(data);
               return true;
           }
       });


        return view;
    }

    public   void  showCustomDialog(int position){
        MysqlOpenHelper mysqlOpenHelper = new MysqlOpenHelper(context);
        SQLiteDatabase sqLiteDatabase = mysqlOpenHelper.getWritableDatabase();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view2 = inflater.inflate(R.layout.dialog_input, null);
        // 创建自定义 Dialog 对象
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(view2);
        // 查找 Dialog 中的 EditText 和按钮控件
        TextView okButton = (TextView) view2.findViewById(R.id.cg);
        TextView cancelButton = (TextView) view2.findViewById(R.id.qx1);
        CheckBox CheckButton = (CheckBox) view2.findViewById(R.id.selectd);
        EditText editText =  view2.findViewById(R.id.content_wait);
        if(position!=-1){
            str =data.get(position).getNeirong();
            editText.setText(str);
        }
        String  str2 = editText.getText().toString();
        CheckButton.setEnabled(false);
        //获取窗口对象
        Window window = dialog.getWindow();
        //设置背景 透明
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置宽高
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置对齐
        window.setGravity(Gravity.BOTTOM);
        //设置圆角
        GradientDrawable shape = new GradientDrawable();
//        radii 数组的前四个值表示左上、右上、右下、左下四个角的圆角半径大小，后四个值表示四个角的扩展长度。
        float[] radii={20,20,20,20,0,0,0,0};
        shape.setCornerRadii(radii);
        window.setBackgroundDrawable(shape);
        // 设置按钮的点击事件
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!str2.isEmpty()){
                    //修改内容操操创建ContentValues对象
                    String str2=editText.getText().toString();
                    ContentValues values= new ContentValues();
                    //插入对应的值会String.valueOf返回当前时间的字符串形式
                    values.put("neirong", str2);
                    values.put("update_time", String.valueOf(new Date()));
                    sqLiteDatabase.update("wait_table",values,"id=?",new String[]{data.get(position).getId()});
                    Toast.makeText(context,"修改成功",Toast.LENGTH_SHORT).show();
                    data=getData();
                    resultListener2.getWaitData(data);
                } else{
                    //创建ContentValues对象
                    String str2=editText.getText().toString();
                    ContentValues values= new ContentValues();
                    //插入对应的值会String.valueOf返回当前时间的字符串形式
                    values.put("neirong", str2);
                    values.put("create_time", String.valueOf(new Date()));
                    sqLiteDatabase.insert("wait_table",null,values);
                    Toast.makeText(context,"添加成功",Toast.LENGTH_SHORT).show();
                    data=getData();
                   resultListener2.getWaitData(data);
                }
                // 关闭 Dialog
                dialog.dismiss();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 关闭 Dialog 清空内容
                dialog.dismiss();
            }
        });
        // 显示 Dialog
        dialog.show();
    }

    @Override
    public void getWaitData(List<waitBean> lists) {

    }

    @Override
    public void getWaitData2(List<waitBean> lists2) {

    }

    public  List<waitBean> getData() {
        MysqlOpenHelper mysqlOpenHelper = new MysqlOpenHelper(context);
        SQLiteDatabase sqLiteDatabase = mysqlOpenHelper.getWritableDatabase();
        String[] ziduan = {"neirong", "create_time", "type", "id"};
        Cursor cursor = sqLiteDatabase.query("wait_table", ziduan, null, null
                , null, null, null);

        ArrayList<waitBean> waitbeas = new ArrayList<>();
        if (cursor.getCount() > 0) {
            int value1 = cursor.getColumnIndex("neirong");
            int value2 = cursor.getColumnIndex("create_time");
            int value3 = cursor.getColumnIndex("type");
            int value4 = cursor.getColumnIndex("id");
            while (cursor.moveToNext()) {
                String nr = cursor.getString(value1);
                String time = cursor.getString(value2);
                int type = cursor.getInt(value3);
                String id = cursor.getString(value4);
                SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日 HH:mm");
                String format = dateFormat.format(new Date(time));
                waitBean waitBean = new waitBean();
                if (type==0){
                    waitBean.setId(id);
                    waitBean.setCreate_time(format);
                    waitBean.setType(String.valueOf(type));
                    waitBean.setNeirong(nr);
                    waitbeas.add(waitBean);
                }
            }
        } else {
            Toast.makeText(context, "wait获取信息失败", Toast.LENGTH_SHORT).show();
        }
        cursor.close();
        return waitbeas;
    }

    public  List<waitBean> getData2() {
        MysqlOpenHelper mysqlOpenHelper = new MysqlOpenHelper(context);
        SQLiteDatabase sqLiteDatabase = mysqlOpenHelper.getWritableDatabase();
        String[] ziduan = {"neirong", "create_time", "type", "id"};
        Cursor cursor = sqLiteDatabase.query("wait_table", ziduan, null, null
                , null, null, null);

        ArrayList<waitBean> waitbeas = new ArrayList<>();
        if (cursor.getCount() > 0) {
            int value1 = cursor.getColumnIndex("neirong");
            int value2 = cursor.getColumnIndex("create_time");
            int value3 = cursor.getColumnIndex("type");
            int value4 = cursor.getColumnIndex("id");
            while (cursor.moveToNext()) {
                String nr = cursor.getString(value1);
                String time = cursor.getString(value2);
                int type = cursor.getInt(value3);
                String id = cursor.getString(value4);
                SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日 HH:mm");
                String format = dateFormat.format(new Date(time));
                waitBean waitBean = new waitBean();
                if (type==1){
                    waitBean.setId(id);
                    waitBean.setCreate_time(format);
                    waitBean.setType(String.valueOf(type));
                    waitBean.setNeirong(nr);
                    waitbeas.add(waitBean);
                }

            }
        } else {
            Toast.makeText(context, "wait获取信息失败", Toast.LENGTH_SHORT).show();
        }
        cursor.close();
        return waitbeas;
    }
    //View需要使用的子控件
    private final  class ViewHolder{
        TextView datewait;
        TextView waitTitle;
        TextView type;
        CheckBox chekbox;
    }


}
